import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertz/jsonserialize/czjl_list/data.dart';
import 'package:fluttertz/pages/czjl/widgets/czjl_row.dart';
import 'package:fluttertz/utils/global.dart';

class Czjl extends StatefulWidget {
  Czjl({Key key}) : super(key: key);

  _CzjlState createState() => _CzjlState();
}

class _CzjlState extends State<Czjl> {
  double bottom = G.screenPadding().bottom;

  AppBar createAppBar() {
    return null;
  }

  static double _nestedScrollOffet = 0;
  final ScrollController _nestedScrollController = new ScrollController();

  List<Widget> czjlListWidgets = [];

  @override
  void initState() {
    _nestedScrollController.addListener(() {
      setState(() {
        _nestedScrollOffet = _nestedScrollController.offset;
      });
    });

    Future.delayed(Duration.zero, () {
      _init(context);
    });

    super.initState();
  }

  _init(BuildContext context) async {
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();
      String url =
          "https://aps.acciot.com.tw/api/taizhu/recepits?id=${G.user.jtinfo.id}";
      response = await dio.get(url);
      if (response == null) {
        return G.toast('獲取操作記錄失敗！');
      }

      CzjlList czjlList = CzjlList.fromJson(response.data);
      int czjlListLen = czjlList.data.length;

      List<Widget> czjlListWidgetsTemp = [];

      czjlList.data.asMap().forEach((int index, CzjlListDatum czjlone) {
        czjlListWidgetsTemp.add(CzjlRow(
          onPress: (BuildContext context, int id) {},
          data: czjlone,
        ));
      });

      setState(() {
        czjlListWidgets = czjlListWidgetsTemp;
      });

      G.loading.hide(context);
    } catch (e) {
      print(e);
      G.loading.hide(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("lib/assets/images/tz/bg.png"),
              fit: BoxFit.cover,
            )),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: customAppbar(
            title: '操作記錄',
            context: context, //null不显示返回按钮
          ),
          body: Stack(
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(left: 25, right: 25),
                child: ListView(
                  padding: EdgeInsets.only(bottom: 80 + bottom),
                  children: czjlListWidgets,
                ),
              ),
            ],
          ),
        ));
  }

}
